<?php
/**
 *  A List of typed items, with sparse numeric indices
 *
 */
class Yz_List
    extends Yz_Class
{

    /** The template object/class, subclass of which all list's entries must be
     *  @var Yz_Class
     */
    protected $object = 'Yz_Class';


    /**
     *  @param object|string class
     *  @param array|null value
     */
    public function __construct( $value, $class )
    {
        if ($class instanceof Yz_Class)
            $class = $class->_class_();
        else
            assert('is_string($class)');
        if (null !== $value) {
            assert('is_array($value)');
            $this->__dict__ = $value;
        }
        $this->object = $class;
    } // __construct


    /** toString()
     *
     *   @return string
     */
    public function __toString()
    {
        $cl = $this->object;
        $count = $this->count();
        return "List<{$cl}>[{$count}]";
    }


    /**
     *  @return string
     */
    public function valueType()
    {
        return $this->object;
    }

    /**
     *  @return string
     */
    public function offsetSet($var, $value)
    {
        throw ImmutableObjectError::_new_();
    }

    /**
     *  @return string
     */
    public function offsetUnset($var)
    {
        throw ImmutableObjectError::_new_();
    }


    /** validate the index, adjust it if required/possible
     *   @param mixed index
     *   @return mixed valid index
     *   @throw TypeError if index not valid
     */
    protected function adjustIndex( $index )
    {
        if (! is_numeric($index))
            throw TypeError::_new_();
        $index = (int)$index;
        if ($index <= 0)
            throw ValueError::_new_();
        return $index;
    }


}
